package com.siyu.tickets.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.siyu.common.page.Pagination;
import com.siyu.common.page.SimplePage;
import com.siyu.common.web.CookieUtils;
import com.siyu.tickets.entity.Partner;
import com.siyu.tickets.entity.PayFlow;
import com.siyu.tickets.entity.SysUser;
import com.siyu.tickets.manager.FinanceMng;
import com.siyu.tickets.manager.PartnerMng;
import com.siyu.tickets.web.SysUtils;

/**
 * 财务管理
 * 
 * @since 2013-7-18
 * @version 0.1
 * @author cuigx
 */
@Controller
public class FinanceAct {

	private static final Logger log = LoggerFactory.getLogger(FinanceAct.class);

	@RequestMapping("/finance/flow")
	public String flow(String action, Date beginTime, Date endTime, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		Partner partner = partnerMng.findById(SysUtils.getUser(request).getId());
		Pagination pagination = manager.getPartnerPayFlowPage(partner, action, beginTime, endTime,
				SimplePage.cpn(pageNo), CookieUtils.getPageSize(request));
		model.addAttribute("action", action);
		model.addAttribute("beginTime", beginTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pagination.getPageNo());
		model.addAttribute("partner", partner);
		return "finance/flow";
	}

	@RequestMapping(value = "/finance/recharge", method = RequestMethod.GET)
	public String recharge(HttpServletRequest request, ModelMap model) {
		return "finance/recharge";
	}

	@RequestMapping(value = "/finance/recharge", method = RequestMethod.POST)
	public String rechargeSubmit(PayFlow flow, HttpServletRequest request, ModelMap model) {
		flow.setOperator(SysUtils.getUser(request));
		manager.recharge(flow);
		log.info("{1}给{2}充值{3}金额成功！", flow.getOperator().getRealname(), flow.getUser().getRealname(), flow.getMoney());
		model.addAttribute("message", "充值成功！");
		return flow("recharge", null, null, null, request, model);
	}

	@RequestMapping("/finance/verifyLog")
	public String ticketVerifyLog(Date beginTime, Date endTime, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		SysUser user = SysUtils.getUser(request);
		Pagination pagination = manager.getTicketVerifyPage(user.getId(), beginTime, endTime, SimplePage.cpn(pageNo),
				CookieUtils.getPageSize(request));
		model.addAttribute("beginTime", beginTime);
		model.addAttribute("endTime", endTime);
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pagination.getPageNo());
		return "finance/verifyLog";
	}

	@Autowired
	private FinanceMng manager;

	@Autowired
	private PartnerMng partnerMng;

}
